<template>
  <div class='container'>
    <el-dialog title="新增目录" :visible.sync="dialogFormVisible" width="30%">
      <el-form :model="form" label-width="100px" :rules="rules" ref="form">
        <el-form-item label="所属学科" v-if="!id">
        <el-select placeholder="请选择" v-model="form.subjectID">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="目录名称" prop="directoryName">
        <el-input
          v-model="form.directoryName"
          autocomplete="off"
          placeholder="请输入目录名称"  
        >
        </el-input>
      </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="yep('form')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { simple } from '@/api/hmmm/subjects'
export default {
  name: 'add-directorys',
  props: ['id'],
  data() {
    return {
      dialogFormVisible: false,
      options: [],
      form: {
        subjectID: null,
        directoryName: null
      },
      rules: {
         directoryName: [
            { required: true, message: '请输入目录名称', trigger: 'blur' }
          ]
      }
    }
  },
  created () {
    this.getOptions()
  },
  methods: {
    async getOptions() {
      const { data } = await simple()
      this.options = data
    },
    yep (ref) {
      this.$refs[ref].validate((valid) => {
        if (valid) {
          this.dialogFormVisible = false
          console.log(this.form)
          this.$emit('handle', this.form)
        } else {
          return false
        }
      })
    },
    cancel () {
      this.dialogFormVisible = false
      this.form.subjectID = null,
      this.form.directoryName = null
    }
  }
}
</script>

<style scoped lang='less'></style>
